package com.v1.mongo;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import com.mongodb.DB;
import com.mongodb.Mongo;
import com.v1.lo.Statement;

public class Mongos {
	private Map<String, Mongo> mongos;
	private Map<String, Properties> dbconfigs;

	private Map<String, DB> dbs;

	public void init() {
		dbs = new HashMap<String, DB>();
		Set<String> keySet = dbconfigs.keySet();
		for (String key : keySet) {
			Properties properties = dbconfigs.get(key);
			Mongo mongo = mongos.get(properties.getProperty("mongo"));
			DB db = mongo.getDB(key);
			boolean authenticate = db.authenticate(properties.getProperty("user"),
					properties.getProperty("password").toCharArray());
			if (!authenticate) {
				throw new RuntimeException("Unable to authenticate with MongoDB server.");
			}

			dbs.put(key, db);
		}
	}

	public DB getDB(Statement s) {
		return dbs.get(s.toString());
	}

	public Map<String, DB> getDbs() {
		return dbs;
	}

	public Map<String, Mongo> getMongos() {
		return mongos;
	}

	public void setMongos(Map<String, Mongo> mongos) {
		this.mongos = mongos;
	}

	public Map<String, Properties> getDbconfigs() {
		return dbconfigs;
	}

	public void setDbconfigs(Map<String, Properties> dbconfigs) {
		this.dbconfigs = dbconfigs;
	}

}
